# @Time:   2021/2/5 9:33
# @Author: Zong Wei

import random
from time import sleep
from faker import Faker
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.wait import WebDriverWait

from ui_automation_testing.test.pageObject.loginPage import LoginPage
from ui_automation_testing.utils.logger import Logger


class ProductPage(LoginPage):
    logger = Logger(__name__).get_log()
    menu = (By.XPATH, "//ul[@class='site-menu']//span[.='品名管理']")
    sub_menu = (By.XPATH, "//ul[@class='site-menu']//span[.='商品管理']")
    search_button = (By.ID, "lr-search")
    add_button = (By.ID, "lr-add")
    edit_button = (By.ID, "lr-edit")
    del_button = (By.ID, "lr-delete")
    save_button = (By.ID, "lr-save")
    check_button = (By.ID, "lr-check")

    remember_code = (By.ID, "RemberCode")
    code = (By.ID, "ProductID")
    name_cn = (By.XPATH, "//select[@id='MaterialNameC']/following::input[1]")
    name_en = (By.ID, "MaterialNameE")
    is_chemical = (By.ID, "IsNotChemicalGoods")
    is_chemical_value = (By.XPATH, "//*[@id='IsNotChemicalGoods-option']//li[3]")
    is_danger = (By.ID, "B_Danger")
    is_danger_value = (By.XPATH, "//*[@id='B_Danger-option']//li[3]")
    is_danger_chemical = (By.ID, "IsDangerChemical")
    is_danger_chemical_value = (By.XPATH, "//*[@id='IsDangerChemical-option']//li[3]")
    unno = (By.XPATH, "//select[@id='UNCode']/following::a[1]")
    unno_values = (By.XPATH, "//select[@id='UNCode']/following::tr[contains(@id,'datagrid-row-r5-2-')]")
    CFS_DangerType = (By.ID, "CFS_DangerType")
    CFS_values = (By.XPATH, "//*[@id='CFS_DangerType-option']//li[position()>1]")
    is_check = (By.ID, "IsCheckFlag")
    export_button = (By.ID, "lr-excel")
    confirm_button = (By.XPATH, "//a[.='确认']")
    first_row = (By.XPATH, "//span[.='助记码']//following::tr[1]")

    def __init__(self):
        self.driver = LoginPage().login()
        self.wait = WebDriverWait(self.driver, 10)

    def product_home(self):
        self.logger.info("登录系统成功")
        self.wait.until(ec.element_to_be_clickable(self.menu)).click()
        self.wait.until(ec.element_to_be_clickable(self.sub_menu)).click()
        sleep(1)
        return self.driver

    def add_product(self):
        self.driver.switch_to.frame(1)
        self.wait.until(ec.element_to_be_clickable(self.add_button)).click()
        self.logger.info("添加商品")
        sleep(2)
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame(2)
        sleep(1)
        self.wait.until(ec.presence_of_element_located(self.remember_code)).send_keys('remember_code')
        self.wait.until(ec.presence_of_element_located(self.name_cn)).send_keys(Faker().name())
        sleep(1)
        self.wait.until(ec.presence_of_element_located(self.name_en)).send_keys(Faker().company())
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.is_chemical)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.is_chemical_value)).click()
        self.wait.until(ec.element_to_be_clickable(self.is_danger)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.is_danger_value)).click()
        self.wait.until(ec.element_to_be_clickable(self.is_danger_chemical)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.is_danger_chemical_value)).click()
        self.wait.until(ec.element_to_be_clickable(self.unno)).click()
        sleep(1)
        random.choice(self.driver.find_elements(*self.unno_values)).click()
        self.wait.until(ec.element_to_be_clickable(self.CFS_DangerType)).click()
        sleep(1)
        random.choice(self.driver.find_elements(*self.CFS_values)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.save_button)).click()
        sleep(1)
        global product_id
        product_id = self.driver.find_element_by_id('ProductID').get_property('value')
        self.logger.info("新增完成")
        print('product_id:' + product_id)

    def search_product(self, option='3'):
        self.driver.switch_to.frame(1)
        self.wait.until(ec.presence_of_element_located(self.code)).send_keys(product_id)
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.is_check)).click()
        sleep(1)
        is_check_value = (By.XPATH, "//*[@id='IsCheckFlag']/option["+option+"]")
        self.wait.until(ec.element_to_be_clickable(is_check_value)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.search_button)).click()
        sleep(2)
        self.logger.info("查询完成")

    def edit_product(self):
        self.search_product()
        self.wait.until(ec.element_to_be_clickable(self.first_row)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.edit_button)).click()
        sleep(1)
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame(2)
        self.wait.until(ec.presence_of_element_located(self.name_cn)).send_keys('+Edit')
        self.wait.until(ec.presence_of_element_located(self.name_en)).send_keys('+Edit')
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.save_button)).click()
        self.logger.info("编辑修改完成")

    def del_product(self):
        self.search_product(option='4')
        self.wait.until(ec.element_to_be_clickable(self.first_row)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.del_button)).click()
        sleep(1)
        self.driver.switch_to.default_content()
        self.wait.until(ec.element_to_be_clickable(self.confirm_button)).click()
        self.logger.info("删除成功")

    def exp_product(self):
        self.search_product(option='4')
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.export_button)).click()
        sleep(2)
        self.logger.info("导出成功")


if __name__ == '__main__':
    pg = ProductPage()
    pg.product_home()
    pg.edit_product()

